﻿
#include <string>
#include <iostream>
#include <thread>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#include <queue>
#include <tuple>
#include <bitset>

class node{
public:
	int32_t p;
	int32_t w;
	node(std::initializer_list<int32_t> list) {
		auto it = list.begin();
		p = *(it++);
		w = *it;
	}
};
constexpr int16_t N = 4;
constexpr int32_t MAX = 9999;
class Solution {
public:
	std::vector<node>  g[N];
	std::vector<int32_t> path;
	
	std::bitset<N> vis;
	int32_t minLength = MAX;

	void calculate(int32_t bPoint,std::vector<int32_t> &outRes,int32_t &outLen) {
		
		vis[bPoint] = true;
		path.emplace_back(bPoint);
		
		dfs(bPoint, 0, 0, outRes);

		outRes.emplace_back(bPoint);
		outLen = minLength;

	}

	void dfs(int32_t i, int32_t count, int32_t length, std::vector<int32_t> &outRes) {
		
		auto link = [&]()->std::tuple<bool,int32_t> {
			for (auto temp : g[i]) {
				if (temp.p == 0) return std::make_tuple(true, temp.w);
			}
			return std::make_tuple(false, -1);
		};

		if (auto [status,len2b] = link(); count == N - 1 && status) {

			minLength = [&](int32_t x, int32_t y)->int32_t {return x < y ? x : y; }(minLength + len2b, length + len2b);
			outRes.clear();
			std::copy(path.begin(), path.end(), std::back_inserter(outRes));
		}

		for (auto j : g[i]) {
			if ( (!vis[j.p]) && length + j.w < minLength) {

				[&]() {vis[j.p] = true; path.emplace_back(j.p); }();
				
				dfs(j.p,count+1,length + j.w,outRes);
				
				[&]() {vis[j.p] = false; path.pop_back(); }();
			}
			
		}

	}

};

int main(int argc, char* argv[]) {
	
	Solution s;
	/*
	s.g[0].push_back(node{ 1, 10 });
	s.g[0].push_back(node{ 2, 6 });
	s.g[0].push_back(node{ 3, 4 });

	s.g[1].push_back(node{ 0, 10 });
	s.g[1].push_back(node{ 2, 5 });
	s.g[1].push_back(node{ 3, 10 });

	s.g[2].push_back(node{ 0, 6 });
	s.g[2].push_back(node{ 1, 5 });
	s.g[2].push_back(node{ 3, 20 });

	s.g[3].push_back(node{ 0, 4 });
	s.g[3].push_back(node{ 1, 10 });
	s.g[3].push_back(node{ 2, 20 });
	*/
	/*s.g[0].push_back(node{ 1, 3 });
	s.g[0].push_back(node{ 2, 1 });
	s.g[0].push_back(node{ 3, 5 });
	s.g[0].push_back(node{ 4, 8 });

	s.g[1].push_back(node{ 0, 3 });
	s.g[1].push_back(node{ 2, 6 });
	s.g[1].push_back(node{ 3, 7 });
	s.g[1].push_back(node{ 4, 9 });

	s.g[2].push_back(node{ 0, 1 });
	s.g[2].push_back(node{ 1, 6 });
	s.g[2].push_back(node{ 3, 4 });
	s.g[2].push_back(node{ 4, 2 });

	s.g[3].push_back(node{ 0, 5 });
	s.g[3].push_back(node{ 1, 7 });
	s.g[3].push_back(node{ 2, 4 });
	s.g[3].push_back(node{ 4, 3 });


	s.g[4].push_back(node{ 0, 8 });
	s.g[4].push_back(node{ 1, 9 });
	s.g[4].push_back(node{ 2, 2 });
	s.g[4].push_back(node{ 3, 3 });
	*/
	s.g[0].push_back(node{ 1, 9 });
	s.g[0].push_back(node{ 2, 19 });
	s.g[0].push_back(node{ 3, 13 });
	
	s.g[1].push_back(node{ 0, 21 });
	s.g[1].push_back(node{ 3, 14 });
	
	s.g[2].push_back(node{ 3, 17 });
	s.g[2].push_back(node{ 0, 1 });


	s.g[3].push_back(node{ 0, 13 });
	s.g[3].push_back(node{ 1, 80 });
	s.g[3].push_back(node{ 2, 10 });


	int min = 9999;
	for (int i = 0; i < N; i++) {
		std::vector<int32_t> res;
		int32_t len;
		s.calculate(2, res, len);
		min = len < min ? len : min;
	}
	
	/*	
	for (auto i : res) {
		std::cout << i << std::endl;
	}
	*/
	std::cout << min << std::endl;
	return 0;
}